# -*- coding: utf-8 -*-
import struct
import os

import struct

# def main():
    # filepath='add-riscv64-nemu.bin'
    # fileout='add-riscv64-nemu.hex'
    # data_file = open(filepath, 'rb')
    # hexx_file = open(fileout, 'w')
    # size=os.path.getsize(filepath)
    # for i in range(size//8):
    #     data0 = data_file.read(1)
    #     data1 = data_file.read(1)
    #     data2 = data_file.read(1)
    #     data3 = data_file.read(1)
    #     data4 = data_file.read(1)
    #     data5 = data_file.read(1)
    #     data6 = data_file.read(1)
    #     data7 = data_file.read(1)
    #     hexx_file.write('{1:02x}{2:02x}{3:02x}{4:02x}{5:02x}{6:02x}{7:02x}{8:02x}\r'.format(i*8,struct.unpack("B",data7)[0],struct.unpack("B",data6)[0],struct.unpack("B",data5)[0],struct.unpack("B",data4)[0],struct.unpack("B",data3)[0],struct.unpack("B",data2)[0],struct.unpack("B",data1)[0],struct.unpack("B",data0)[0]))
    # data_file.close()
    # hexx_file.close()

def main():
    filepath='add-riscv64-nemu.bin'
    fileout='add-riscv64-nemu.hex'
    # filepath='dummy-riscv64-nemu.bin'
    # fileout='dummy-riscv64-nemu.hex'    
    data_file = open(filepath, 'rb')
    hexx_file = open(fileout, 'w')
    size=os.path.getsize(filepath)
    for i in range(size//4):
        data0 = data_file.read(1)
        data1 = data_file.read(1)
        data2 = data_file.read(1)
        data3 = data_file.read(1)
        hexx_file.write('{1:02x} {2:02x} {3:02x} {4:02x}\r'.format(i*4,struct.unpack("B",data0)[0],struct.unpack("B",data1)[0],struct.unpack("B",data2)[0],struct.unpack("B",data3)[0]))
    data_file.close()
    hexx_file.close()

if __name__ == '__main__':
    main()
